package com.cskaoyan.com.ex;

public class Ex3 {
    public static void main(String[] args) {

        //        反转单链表
        Node e = new Node("e", null);
        Node d = new Node("d", e);
        Node c = new Node("c", d);
        Node b = new Node("b", c);
        Node a = new Node("a", b);
        // a --> b --> c  --> d --> e


        // e  --> d  -> c  --> b  --> a
        // relinkedHead经过翻转链表的头结点
        Node relinkedHead = relinked(a);

        System.out.println(relinkedHead);
    }

    /**
     * 给定一个链表,  得到一个翻转链表
     * @param head: 给定链表的头结点
     * @return: 翻转链表的头结点
     */
    private static Node relinked(Node head) {
        // 翻转链表
        Node reLinked = null; // 经过翻转链表

        Node mid = head;  // 定义一个临时标记结点

        while (mid != null){
            Node next = mid.next;
            mid.next = reLinked;
            reLinked = mid;
            mid = next;
        }

        return reLinked;
    }
}
